package com.evil.application.pojo.dto.robot;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.evil.application.pojo.dto.robot.setting.RepeatSettingDTO;
import com.evil.application.pojo.dto.robot.setting.RobotTriggerSettingDTO;
import com.evil.application.pojo.entity.ApplicationRobot;
import com.evil.common.application.dto.judge.JudgeOr;
import com.evil.common.application.dto.robot.RobotRuleDTO;
import com.evil.common.application.enums.robot.RobotFirstTriggerTypeEnum;
import com.evil.common.application.enums.robot.RobotRepeatTypeEnum;
import com.evil.common.application.enums.robot.RobotTriggerTypeEnum;
import com.evil.common.core.enums.SwitchEnum;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 机器人信息 实体
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Data
public class ApplicationRobotDTO {

    public static final String[] ignoreField = new String[]{"triggerSetting", "repeatSetting", "recalculationControlIds"};

    /**
     * 机器人名称
     */
    @NotBlank(message = "机器人名称不能为空")
    private String robotName;

    /**
     * 首次触发时间 {@link RobotFirstTriggerTypeEnum}
     */
    @NotNull(message = "首次触发时间不能为空")
    private Integer firstTriggerType;

    /**
     * 触发条件配置
     */
    @Valid
    private RobotTriggerSettingDTO triggerSetting;

    /**
     * 重复周期 {@link RobotRepeatTypeEnum}
     */
    @NotNull(message = "重复周期不能为空")
    private Integer repeatType;

    /**
     * 重复周期配置
     */
    @Valid
    private RepeatSettingDTO repeatSetting;

    /**
     * 触发动作 {@link RobotTriggerTypeEnum}
     */
    @NotNull(message = "触发动作不能为空")
    private Integer triggerType;

    /**
     * 目标应用
     */
    @NotNull(message = "目标应用不能为空")
    private Long targetApplicationId;

    /**
     * 是否开启重新触发数据关联或公式(0:否, 1:是)
     */
    @NotNull(message = "是否开启重新触发数据关联或公式参数不能为空")
    private Integer recalculation;

    /**
     * 重新触发数据关联或公式的控件id集合
     */
    private List<Long> recalculationControlIds;

    /**
     * 机器人状态(0:关, 1:开)
     */
    @NotNull(message = "机器人状态不能为空")
    private Integer robotStatus;

    /**
     * 排序
     */
    private Integer sort;

    /**
     * 数据范围
     */
    @Valid
    private List<JudgeOr> rangeJudges;

    /**
     * 目标应用 筛选数据
     */
    @Valid
    private List<JudgeOr> judgeOrs;

    /**
     * 机器人规则
     */
    @Valid
    private List<RobotRuleDTO> robotRules;

    /**
     * 同步(0:关, 1:开)
     */
    private Integer sync;

    public ApplicationRobotDTO() {
        this.firstTriggerType = RobotFirstTriggerTypeEnum.ON_ADD.getId();
        this.repeatType = RobotRepeatTypeEnum.NO.getId();
        this.triggerSetting = new RobotTriggerSettingDTO();
        this.repeatSetting = new RepeatSettingDTO();
        this.recalculation = SwitchEnum.NO.getId();
        this.recalculationControlIds = new ArrayList<>();
        this.sort = 0;
        this.judgeOrs = new ArrayList<>();
        this.sync = SwitchEnum.NO.getId();
    }

    public ApplicationRobotDTO(ApplicationRobot applicationRobot) {
        BeanUtil.copyProperties(applicationRobot, this, ignoreField);

        if (StringUtils.isNotBlank(applicationRobot.getTriggerSetting())) {
            this.triggerSetting = JSON.parseObject(applicationRobot.getTriggerSetting(), RobotTriggerSettingDTO.class);
        }
        if (StringUtils.isNotBlank(applicationRobot.getRepeatSetting())) {
            this.repeatSetting = JSON.parseObject(applicationRobot.getRepeatSetting(), RepeatSettingDTO.class);
        }
        if (StringUtils.isNotBlank(applicationRobot.getRecalculationControlIds())) {
            this.recalculationControlIds = JSON.parseArray(applicationRobot.getRecalculationControlIds(), Long.class);
        }
    }

    public void copyProp(ApplicationRobot applicationRobot) {
        applicationRobot.setRobotName(this.robotName);
        applicationRobot.setFirstTriggerType(this.firstTriggerType);
        applicationRobot.setTriggerSetting(JSON.toJSONString(this.triggerSetting));
        applicationRobot.setRepeatType(this.repeatType);
        applicationRobot.setRepeatSetting(JSON.toJSONString(this.repeatSetting));
        applicationRobot.setTriggerType(this.triggerType);
        applicationRobot.setTargetApplicationId(this.targetApplicationId);
        applicationRobot.setRecalculation(this.recalculation);
        applicationRobot.setRecalculationControlIds(JSON.toJSONString(this.recalculationControlIds));
        applicationRobot.setRobotStatus(this.robotStatus);
        applicationRobot.setSort(this.sort);
        applicationRobot.setSync(this.sync);
    }
}
